package com.bawei.live.view

import android.app.AlertDialog
import android.content.Intent
import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.base.RoutePath
import com.bawei.common.entity.SimpleVideoEntity
import com.bawei.common.mvi.BaseMviFragment
import com.bawei.live.R
import com.bawei.live.adapter.HotLiveAdapter
import com.bawei.live.adapter.LiverListAdapter
import com.bawei.live.adapter.TypeListAdapter
import com.bawei.live.databinding.FragmentLiveBinding
import com.bawei.live.entity.LiverEntity
import com.bawei.live.entity.TypeEntity
import com.bawei.live.intent.LiveViewIntent
import com.bawei.live.state.LiveUiState
import com.bawei.live.vm.LiveViewModel
import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
@Route(path = RoutePath.PATH_FRAGMENT_LIVE)
class LiveFragment : BaseMviFragment<FragmentLiveBinding>() {

    private val mViewModel: LiveViewModel by activityViewModels()

    private lateinit var mLiverAdapter: LiverListAdapter
    private lateinit var mTypeAdapter: TypeListAdapter
    private lateinit var mHotLiveAdapter: HotLiveAdapter

    private var liverList = mutableListOf<LiverEntity>()
    private var typeList = mutableListOf<TypeEntity>()
    private var hotLiveList = mutableListOf<SimpleVideoEntity>()

    private val searchHotWords = listOf("视频", "青年", "热门视频", "搞笑视频")
    override fun getLayoutId(): Int = R.layout.fragment_live

    override fun initViews() {
//        mDataBinding.btnLive.setOnClickListener{
//            val intent = Intent(requireContext(), ZhiboActivity::class.java)
//            startActivity(intent)
//        }
<<<<<<< HEAD
=======
//        <<<<<<< HEAD
//        =======

>>>>>>> 4bd9ee2c4d9f0e81098ef6d1abae9f2eb7ec6c07

        mDataBinding.buttonShowDialog.setOnClickListener {
            showAlertDialog()
        }

        //设置搜索热词
        mDataBinding.tvbSearch.setDatas(searchHotWords)

        //初始化主播列表
        mDataBinding.rvLiver.layoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false)
        mLiverAdapter = LiverListAdapter(liverList)
        mDataBinding.rvLiver.adapter = mLiverAdapter

        //初始化热门分类列表
        mDataBinding.rvHotType.layoutManager = GridLayoutManager(requireContext(), 4, GridLayoutManager.VERTICAL, false)
        mTypeAdapter = TypeListAdapter(typeList)
        mDataBinding.rvHotType.adapter = mTypeAdapter

        //初始化热门直播列表
        mDataBinding.rvHotLive.layoutManager = GridLayoutManager(requireContext(), 2, GridLayoutManager.VERTICAL, false)
        mHotLiveAdapter = HotLiveAdapter(hotLiveList)
        mHotLiveAdapter.setOnItemClickListener(object : HotLiveAdapter.OnItemClickListener{
            override fun onItemChildClick(v: View?, position: Int) {
<<<<<<< HEAD
                ARouter.getInstance().build(RoutePath.PATH_LIVE_DETAIL_PAGE)
//                    .withInt("typeId", -1)
//                    .withString("channelId", "")
                    .withSerializable("videoEntity", hotLiveList[position])
=======


                ARouter.getInstance().build(RoutePath.PATH_ZHIBO_PAGE)
>>>>>>> 76ff2322edeacda46edf474e6275a766d8cc9b2d
                    .navigation()
            }
        })

        mDataBinding.rvHotLive.adapter = mHotLiveAdapter

        //本地模拟直播和分类列表
        initLocData()
    }

    private fun showAlertDialog(){
        // 创建 AlertDialog.Builder 对象
        val builder = AlertDialog.Builder(context)
        //builder.setTitle("提示") // 设置标题
        builder.setMessage("广告标题") // 设置内容
        builder.setView(R.layout.dialog)

        // 设置确认按钮
        builder.setPositiveButton("确定") { dialog, which ->
            // 点击确定后的操作
            dialog.dismiss() // 关闭弹窗
        }

        // 设置取消按钮
        builder.setNegativeButton("取消") { dialog, which ->
            // 点击取消后的操作
            dialog.dismiss() // 关闭弹窗
        }

        // 创建并显示弹窗
        val dialog: AlertDialog = builder.create()
        dialog.show()
    }

    private fun initLocData(){
        //模拟主播数据
        liverList.add(LiverEntity(0, R.drawable.shape_liver_bg_1,"https://sf3-cdn-tos.bdxiguastatic.com/img/pgc-image/790a9c2e98b9463480cb267d95d07f86~300x300.image", "小橘子"))
        liverList.add(LiverEntity(1, R.drawable.shape_liver_bg_1,"https://sf3-cdn-tos.bdxiguastatic.com/img/pgc-image/790a9c2e98b9463480cb267d95d07f86~300x300.image", "小橘子"))
        liverList.add(LiverEntity(2, R.drawable.shape_liver_bg_1,"https://sf3-cdn-tos.bdxiguastatic.com/img/pgc-image/790a9c2e98b9463480cb267d95d07f86~300x300.image", "小橘子"))
        liverList.add(LiverEntity(3, R.drawable.shape_liver_bg_1,"https://sf3-cdn-tos.bdxiguastatic.com/img/pgc-image/790a9c2e98b9463480cb267d95d07f86~300x300.image", "小橘子"))
        liverList.add(LiverEntity(4, R.drawable.shape_liver_bg_1,"https://sf3-cdn-tos.bdxiguastatic.com/img/pgc-image/790a9c2e98b9463480cb267d95d07f86~300x300.image", "小橘子"))
        mLiverAdapter.notifyDataSetChanged()

        //模拟热门分类数据
        typeList.add(TypeEntity(0, R.drawable.baseline_videogame_asset_24, "游戏区"))
        typeList.add(TypeEntity(1, R.drawable.baseline_videogame_asset_24, "音乐区"))
        typeList.add(TypeEntity(2, R.drawable.baseline_videogame_asset_24, "生活区"))
        typeList.add(TypeEntity(3, R.drawable.baseline_videogame_asset_24, "懂车帝"))
        mTypeAdapter.notifyDataSetChanged()

    }

    override suspend fun sendViewIntent() {
        mViewModel.channel.send(LiveViewIntent.LoadRecommendSimpleVideo(1, 10))
    }

    override suspend fun collectUiState() {
        mViewModel.uiState.collect {
            when (it) {
                is LiveUiState.LoadSimpleVideoSuccess -> {
                    hotLiveList.addAll(it.list)
                    mHotLiveAdapter.notifyDataSetChanged()
                }
                is LiveUiState.LoadSimpleVideoFail -> showToastMsg(it.msg)
                else -> {}
            }
        }

    }

}